#include "hx71x.h"

/* you need edit this code ,port to other project */
#ifdef __SDCC
#define hx71x_sck P1_7
#define hx71x_dt P1_6
#else
sbit hx71x_sck = P1^7;
sbit hx71x_dt = P1^6;
#endif

/* default profile
 * A通道，128增益
 */

/* delay */
void hx71x_delay(void)
{
    _nop_();
    _nop_();
}


/* read data for hx71x */
ulong hx71x_read(void)
{
    uchar i;
    ulong value;

    hx71x_dt = 1;
    hx71x_delay();
    hx71x_sck = 0;
    value = 0;
    while(hx71x_dt);

    for(i = 0; i < 24; i ++)
    {
        hx71x_sck = 1;
        value = (value) <<1;
	    hx71x_sck = 0;
        if(hx71x_dt)
		    (value)++;
    }

    /* A通道 增益128 */
    hx71x_sck = 1;
    value = (value) ^ 0x800000;//第25个脉冲下降沿来时，转换数据
    hx71x_delay();
    hx71x_sck = 0;

    return value;
}

float hx71x_get_weight(ulong value, uchar unit)
{    
    if(unit == HX71X_UNIT_LBS)
        return 1.0 * value/104.11 * 2.2/1000;
    return 1.0 * value/104.11/1000;
}
